// SubnegotiationCommand.cpp: implementation of the CSubnegotiationCommand class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "AnsiTerminal.h"
#include "SubnegotiationCommand.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CSubnegotiationCommand::CSubnegotiationCommand(ITelnetOptionCollection *pOptionCol)
{
	m_pOption = NULL;
	m_pOptionCol = pOptionCol;
	m_bEnd = FALSE;
	m_prevIAC = FALSE;
}

CSubnegotiationCommand::~CSubnegotiationCommand()
{
}

BOOL CSubnegotiationCommand::EatChar(char c)
{
	if(m_bEnd)
	{
		return FALSE;
	}
	if(m_pOption)
	{
		if(m_prevIAC)
		{
			if(c == IAC)
			{
				m_parameters.Add(IAC);
				m_prevIAC = FALSE;
			}
			else if(c == SE)
			{
				m_bEnd = TRUE;
				m_pOption->SetParameters(m_parameters.GetData(), m_parameters.GetSize());
			}
			else
			{
				m_parameters.RemoveAll();
				return FALSE; // something wrong..
			}
		}
		else if(c == IAC)
		{
			m_prevIAC = TRUE;
		}
		else
		{
			m_parameters.Add(c);
		}
	}
	else
	{
		m_pOption = m_pOptionCol->FindOption(c);
	}
	return TRUE;
}
